package com;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author : wushikai
 * <p>
 * date : 2021-03-02
 */
public class NumberDemo {


    public static void main(String[] args) throws ParseException {

        DecimalFormat df   = new DecimalFormat("######0.00");
        double d1 = 3.23456 ;
        double d2 = 0.0;
        double d3 = 2.0;
        System.out.println( df.format(d1)  );
        System.out.println(df.format(d2));
        System.out.println(df.format(d3/d1  * 100));

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse("2021-02-28");

        Calendar calendar= Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);  // 2021-02-27
        Date endDate = calendar.getTime();
        System.out.println(simpleDateFormat.format(endDate));
        calendar.add(Calendar.DATE , -6); // 2021-02-21

        Date startDate = calendar.getTime();
        System.out.println(simpleDateFormat.format(startDate));
        calendar.add(Calendar.DATE , -1);
        Date ratioStartDate = calendar.getTime();
        System.out.println(simpleDateFormat.format(ratioStartDate));

        calendar.add(Calendar.DATE , -6);
        Date ratioEndDate = calendar.getTime();
        System.out.println(simpleDateFormat.format(ratioEndDate));




        Double d = null ;

        System.out.println(     getCycleRatio(  1.0, 2.0 ) );




    }


    /**
     * @param d1 本周参数
     * @param d2 上周参数
     *
     * @return 环比 理论来说 范围在 [-100 , +∞)
     * */
    private static String getCycleRatio(Double d1, Double d2){
        DecimalFormat df   = new DecimalFormat("######0.00");
        String ratio;
        try {

            ratio = df.format((d1 - d2) / d2 * 100);

        }catch (Exception e){
            e.printStackTrace();
            System.out.println((e.getMessage()  +   d1+" "+ d2 ) );
            return "-";
        }
        return  ratio;

    }



    public static void main1(String[] args){


        for (int i = 1; i <= 7; i++) {

            System.out.println(i);
        }
    }



}
